package sdk.webview.fmc.com.fmcsdk.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.preference.PreferenceManager;
import android.util.Log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




/**
 * Created by Administrator on 2016/7/28 0028.
 */
public class FMCLog {
	public static int TRACE=5;
    public static int DEBUG=4;
    public static int INFO=3;
    public static int WARN=2;
    public static int ERROR=1;
    private static FMCLog fmclog;
    private static Logger logger;
    private Context context;
    SharedPreferences spf;
    private static final Object object = new Object();
    private FMCLog(Context context){
       logger= LoggerFactory.getLogger("fmc");
       spf= PreferenceManager.getDefaultSharedPreferences(context);
        /**使用AppLicationContext防止单例模式内存泄漏*/
        this.context = context.getApplicationContext();
    }
    public static FMCLog getInstance(Context context){
        if (fmclog==null||logger==null)
            synchronized (object){
                fmclog=new FMCLog(context);
            }

        return fmclog;
    }
    public void trace(String format, Object arg){
        if(spf.getInt(Constant.LOGLEVEL,INFO)>=TRACE){
        	StackTraceElement element=new Exception().getStackTrace()[1];
            logger.trace("【"+format+"】"+arg+":--------"+element,"");
        }
    }
    public void debug(String format, Object arg){
        if(spf.getInt(Constant.LOGLEVEL,INFO)>=DEBUG){
            StackTraceElement element=new Exception().getStackTrace()[1];
            logger.debug("【"+format+"】"+arg+":---------"+element,"");
            Log.d("【"+format+"】",arg.toString());
        }
    }
    public void warn(String format, Object arg){
        if(spf.getInt(Constant.LOGLEVEL,INFO)>=WARN){
        	StackTraceElement element=new Exception().getStackTrace()[1];
            if (arg instanceof Throwable){
                Throwable ex = (Throwable) arg;
                logger.warn("【"+format+"】"+getCrashReport(ex)+":--------"+element, "");
            }else{
                logger.warn("【"+format+"】"+arg+":--------"+element, "");
            }
        }
    }
    public void info(String format, Object arg){
        if(spf.getInt(Constant.LOGLEVEL,INFO)>=INFO){
        	StackTraceElement element = new Exception().getStackTrace()[1];
            logger.info("【"+format+"】"+arg+":---------"+element, "");
        }
    }
    public void error(String format, Object arg){
        if(spf.getInt(Constant.LOGLEVEL,INFO)>=ERROR){
        	StackTraceElement element=new Exception().getStackTrace()[1];
            logger.error("【"+format+"】"+arg+":---------"+element, "");
        }
    }
    private String getCrashReport(Throwable ex) {
        StringBuffer exceptionStr = new StringBuffer();
        try {
            PackageInfo pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            exceptionStr.append("Version: "+pinfo.versionName+"("+pinfo.versionCode+")\n");
            exceptionStr.append("Android: "+android.os.Build.VERSION.RELEASE+"("+android.os.Build.MODEL+")\n");
            exceptionStr.append("Exception: "+ex.getMessage()+"\n");
            StackTraceElement[] elements = ex.getStackTrace();
            for (int i = 0; i < elements.length; i++) {
                exceptionStr.append(elements[i].toString()+"\n");
            }
        } catch (Exception e) {
            return  ex.getMessage();
        }
        return exceptionStr.toString();
    }
}
